package org.demo.honor.representation.controller;

import lombok.RequiredArgsConstructor;
import org.demo.honor.convert.OrderConvert;
import org.demo.honor.pojo.request.CreateOrderRequest;
import org.demo.honor.pojo.response.OrderListResponse;
import org.demo.honor.pojo.response.OrderResponse;
import org.demo.honor.service.OrderService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @GetMapping(value = "/{id}")
    public ResponseEntity<OrderListResponse> retrieveOrder(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(OrderListResponse.builder()
            .orderResponse(OrderConvert.INSTANCE.covertEntity2Rep(orderService.retrieveOrder(id)))
            .build());
    }

    @PostMapping
    public ResponseEntity<OrderResponse> createOrder(@RequestBody CreateOrderRequest createOrderRequest) {
        return ResponseEntity.ok(
            OrderConvert.INSTANCE.covertEntity2Rep(orderService.createOrder(createOrderRequest))
        );
    }
}
